<?php
namespace Home\Controller;
use Think\Controller;
class AdduserController extends Controller {
    public function _initialize() {
        if(!session('admin')) {
            $this->redirect('Public/adlogin');
        }
    }
    public function index() {
        
        $this->users = D('user')->select();
        $this->display();
    }
    public function add() {
        if (IS_POST) {
           $this->doAdd(); 
        }
        $this->display();
    }
    private function doAdd() {
        $data['uname'] = I('post.uname');
        $data['upass'] = md5(I('post.upass'));
        $data['msgname'] = I('post.msgname');
        $data['msgpass'] = I('post.msgpass');
        $data['msgapikey'] = I('post.msgapikey');
        $data['orgseq'] = I('post.orgseq');
        if (D('user')->addUser($data)) {
           $this->success('添加成功', U('Adduser/index')); exit();
        } else {
            $this->error('添加失败', U('Adduser/add'));
        }
    }
    public function del() {
        $where['id'] = intval(I('get.uid'));
        if (D('User')->delUser($where)) {
           D('Expiremsg')->where(array('uid'=>$where['id']))->delete();
           D('Signature')->where(array('uid'=>$where['id']))->delete();
           echo json_encode(array('msg'=>'success'));
        } else {
            echo json_encode(array('msg'=>'error'));
        }
    }
}
